<?php
require_once("common.php");
include S_ROOT."include/image.class.php";
include(S_ROOT."data/dataconfig.php");

if(empty($_GET['job'])){
	$id = $_GET['id'];
	/**
	 * 根据查询条件，计算当前图片所在页面，并生产下一个图片的链接地址
	 **/
	$pageSize = empty($_CONFIG['imageNum']) ? 5 : $_CONFIG['imageNum'];
	$order  = empty($_GET['order']) ? "createtime" : $_GET['order'];
	
	if(!empty($_GET['author'])){
		$where .= " AND author LIKE '%".$_GET['author']."%'";
	}
	if(!empty($_GET['title'])){
		$where .= " AND title LIKE '%".$_GET['title']."%'";
	}

	if(!empty($id)){
		$order  = empty($_GET['order']) ? "createtime" : $_GET['order'];
		$query = $db->query("SELECT id,imageurl,thumburl FROM ".tname('image')."  WHERE ifdelete=0  $where ORDER BY $order DESC");

		$i = 0;
		$flag = true;
		while($array=$db->fetch_array($query)){
			$i++;
			$nextId = $array['id'];
			if($flag==false){
				break;
			}
			if($id==$array['id']){
				$flag = false;
			}
		}
		if($nextId!=$id){
			$page = ceil(($i-1)/$pageSize);
		}else{
			$page = ceil($i/$pageSize);
		}
		/**
		 * 下一张图片
		 **/
		if($id!=$nextId){
			$nextHref = "photo.php?id=$nextId&order=$order";
		}else{
			$nextHref = "javascript:alert('已经是最后一个作品');";
		}
	}else{
		$page = empty($_GET['page']) ? 1 : $_GET['page'];

	}


	
	/**
	 * 作品列表
	 **/
	
	$limit = " LIMIT ".($page-1)*$pageSize.",".$pageSize;
	$query = $db->query("SELECT id,imageurl,thumburl FROM ".tname('image')." WHERE ifdelete=0 $where  ORDER BY $order DESC $limit");

	$list = $db->fetch_all($query);
	$count = $db->get_one("SELECT count(*) as amount FROM ".tname('image')." WHERE ifdelete=0 $where ORDER BY $order DESC  ");
	$pagenum = ceil($count['amount']/$pageSize);

	if(sizeof($list)<=0){
		showmessage("对不起，没有找到相关作品！");
	}

	/**
	 * 下一张图片
	 **/
	if(empty($id)){
		$nextId=$list[1]['id'];
		$nextHref = "photo.php?id=$nextId&order=$order&author=".urlencode($_GET['author'])."&title=".urlencode($_GET['title']);
	}

	/**
	 * 当前展品
	 **/
	$id = $_GET['id'];
	!$id && $id=$list[0]['id'];
	$photo = $db->get_one("SELECT * FROM ".tname('image')."  WHERE id='$id' ");

	/**
	 * 按照比例显示照片
	 **/
	$url = "photo.php?page=$page&pageSize=$pageSize&id=$id&order=$order&author=".urlencode($_GET['author'])."&title=".urlencode($_GET['title']);
	$imageInfo = image::info(S_ROOT.$photo['imageurl']);
	$systemWidth = $_CONFIG['photowidth'];
	$systemHeight = $_CONFIG['photoheight'];

//	echo $systemWidth."===".$systemHeight;

	$percent = $imageInfo['width']/$imageInfo['height'];
	if($imageInfo['width']>$systemWidth){
		$photoWidth = $systemWidth;
		$photoHeight = $systemWidth/$percent;
		if($photoHeight>$systemHeight){
			$photoHeight = $systemHeight;
			$photoWidth = $systemHeight*$percent;
		}
	}else{
		$photoHeight = $systemHeight;
		$photoWidth = $systemHeight*$percent;
		if($photoWidth>$systemWidth){
			$photoWidth = $systemWidth;
			$photoHeight = $systemWidth/$percent;
		}
	}

	/**
	 * 评论
	 **/
	$commentPage =  empty($_GET['commentpage']) ? 1 : $_GET['commentpage'];
	$commentPageSize = 5;
	$commentLimit = " LIMIT ".($commentPage-1)*$commentPageSize.",".$commentPageSize;
	$query = $db->query("SELECT content,createtime,id,ifhide  FROM ".tname('comment')." WHERE imageid='$id' $commentLimit");
	$commentList = $db->fetch_all($query);

	$commentCount = $db->get_one("SELECT COUNT(*) AS amount from  ".tname('comment')." WHERE imageid='$id'  ");

	$commentPage = multi($commentCount['amount'],$commentPageSize,$commentPage,$url,"comment");

	
	/**
	 * 公告列表
	 **/
	$query = $db->query("SELECT title,id,posttime FROM ".tname('news')." ORDER BY posttime DESC");
	$newsList = $db->fetch_all($query);


	include template("photo");
}else if($_GET['job']=="getnum"){
	$id = $_GET['id'];
	$image = $db->get_one("SELECT votenum FROM ".tname('image')." WHERE id='$id'");
	echo $image['votenum'];
}
?>